package com.jl.cloud.commom.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;


/**
 * Redis自动注入配置
 * @author liang.jun
 * @ConditionalOnExpression 和自动注入的POM依赖是冲突的，两者只能存在一个
 */
@Configuration
@Slf4j
@ConditionalOnExpression("${config.redis.enabled:false}")
public class RedisConfig {

    @Value("${redis.host}")
    private String redisHost;

    @Value("${redis.port}")
    private  String redisPort;

    @Value("${redis.password}")
    private  String password;


    /**
     * redis分布式客户端
     * 默认单节点注入
     * @return
     */
    @Bean
    public RedissonClient getRedisson(){
        RedissonClient redissonClient = null;
        try{
            Config config = new Config();
            config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort).setPassword(password);
            redissonClient =  Redisson.create(config);
            log.info("======加载redissonClient成功  {} , {} ======",redisHost,redisPort);
        }catch (Exception e){
            log.error("======加载redissonClient失败======",e);
        }
        return redissonClient;
    }



}
